<?php


class ExpertController extends BaseController
{
    /*
    * csrf过滤器
    */
    public function __construct()
    {
        $this->beforeFilter('auth',array('except'=>array('login','Dologin')));
        $this->beforeFilter('csrf',array('on'=>'post'));
    }

    /**
     * @return mixed后台首页
     */

    public function getIndex()
    {
        $experts = Expert::paginate(10);
        return View::make('admin.expert.list')->with('experts',$experts);
    }

    /**
     * 添加用户
     */
    public function getAddExpert()
    {
        return View::make('admin.expert.add');
    }

    public function postAddExpert()
    {
        //表单验证
        $data =Input::all();
        $rules =array(
            'name'  		=> 'required',
            'photo'  		=> 'required',
            'title'  		=> 'required',
            'postion'  		=> 'required',
            'department'    => 'required',
            'hospital'  	=> 'required',
            'description'   => 'required',
            'education'   => 'required'
        );
        $validator = Validator::make($data,$rules);
        if($validator->fails())
        {
            $errors = $validator->messages();
            return Redirect::to('/adm/user/add-expert')->withErrors($errors);
        }



        $name=Input::get('name');
        $photo=Input::get('photo');
        $title=Input::get('title');
        $postion=Input::get('postion');
        $dapartment=Input::get('dapartment');
        $hospital=Input::get('hospital');
        $description=Input::get('description');
        $education=Input::get('education');
        $expert =new Expert();
        $expert->name=$name;
        $expert->photo=$photo;
        $expert->title=$title;
        $expert->postion=$postion;
        $expert->dapartment=$dapartment;
        $expert->hospital=$hospital;
        $expert->description=$description;
        $expert->education=$education;
        $expert->save();
        return Redirect::to('/adm/expert');
    }

    public function getEditExpert($id)
    {
        $expert = Expert::find($id);
        return View::make('admin.expert.edit')->with('expert',$expert);
    }
    public function postEditExpert()
    {
        $id=Input::get('id');
        $name=Input::get('name');
        $photo=Input::get('photo');
        $title=Input::get('title');
        $postion=Input::get('postion');
        $dapartment=Input::get('dapartment');
        $hospital=Input::get('hospital');
        $description=Input::get('description');
        $education=Input::get('education');
        $expert =new Expert();
        $expert->name=$name;
        $expert->photo=$photo;
        $expert->title=$title;
        $expert->postion=$postion;
        $expert->dapartment=$dapartment;
        $expert->hospital=$hospital;
        $expert->description=$description;
        $expert->education=$education;
        $expert->save();
        return Redirect::to('/adm/expert');
    }

    public function postDeleteExpert()
    {
        $id=Input::get("id");
        $expert=Expert::find($id);
        $expert->delete();
        return "删除成功！";
    }


}